/*
玩家积分改变界面
*/
var confige = require("confige");
cc.Class({
    extends: cc.Component,

    properties: {
        history_item:{
            default:null,
            type:cc.Node
        },
        isInit:false,
    },

    onLoad: function () {

    },

    onInit:function(){
        this.historyContent = this.node.getChildByName("historyView").getChildByName("view").getChildByName("content");
        this.historyItemBeginY = 0;
        this.historyItemOffsetY = -65;
        this.historyItemList = {};
        this.curHistoryData = {};
        this.isInit = true;
    },
    onShareBtnClick:function()
    {
        window.DKTools.audioEG.playEffect("tips_btnClick");
        if(cc.sys.platform == cc.sys.MOBILE_BROWSER){
            var self = this;
            console.log("H5分享给好友");
            var curShareURL = confige.h5ShareUrlNew.replace('ROOMNUM', '0');
            if(confige.h5InviteCode != "0")
            {
                curShareURL += "&invite_code=" + confige.h5InviteCode;
            }
            wx.onMenuShareAppMessage({
                title: self.shareTitle,
                desc: self.shareDes,
                link: curShareURL,
                imgUrl: confige.h5ShareIco,
                trigger: function(res) {},
                success: function(res) {},
                cancel: function(res) {},
                fail: function(res) {}
            });
            console.log("H5分享到朋友圈2222222");
            wx.onMenuShareTimeline({
                title: self.shareTitle,
                desc: self.shareDes,
                link: curShareURL,
                imgUrl: confige.h5ShareIco,
                trigger: function(res) {},
                success: function(res) {},
                cancel: function(res) {},
                fail: function(res) {}
            });

        }
        if (!cc.sys.isNative) return;
        let dirpath = jsb.fileUtils.getWritablePath() + 'ScreenShoot/';
        if (!jsb.fileUtils.isDirectoryExist(dirpath)) {
            jsb.fileUtils.createDirectory(dirpath);
        }
        let name = 'ScreenShoot-' + (new Date()).valueOf() + '.png';
        let filepath = dirpath + name;
        let size = cc.winSize;
        let rt = cc.RenderTexture.create(size.width, size.height,cc.Texture2D.PIXEL_FORMAT_RGBA8888, gl.DEPTH24_STENCIL8_OES);
        cc.director.getScene()._sgNode.addChild(rt);
        rt.setVisible(false);
        rt.begin();
        cc.director.getScene()._sgNode.visit();
        rt.end();
        rt.saveToFile('ScreenShoot/' + name, cc.ImageFormat.PNG, true, function() {
            cc.log('save succ');
            rt.removeFromParent();
            if(confige.curUsePlatform == 1)
            {
                jsb.reflection.callStaticMethod("org/cocos2dx/javascript/JSCallJAVA", "JAVALog", "(Ljava/lang/String;)V", "filepath222==="+filepath);
                jsb.reflection.callStaticMethod("org/cocos2dx/javascript/JSCallJAVA", "WXShareScreenPath", "(Ljava/lang/String;)V", filepath);
            }else if(confige.curUsePlatform == 2){
                jsb.reflection.callStaticMethod("JSCallOC","WXShareScreenWithPath:",filepath);
            }
        });
    },

    updateHistory:function(){
        for(var i in this.historyItemList)
        {
            this.historyItemList[i].destroy();
        }
        this.historyItemList = {};

        var newContentHeight = 0;
        var historyItemCount = 0;
        var newHistoryItemY = this.historyItemBeginY;
        var newDataList = [];
        for(var i in this.curHistoryData)
            newDataList[i] = this.curHistoryData[i];
        newDataList = window.DKTools.quickSort(newDataList,"time");
        console.log(newDataList);
        for(var i in newDataList)
        {
            historyItemCount++;

            var curRoomData = newDataList[i];
            // console.log("curRoomData======");
            // console.log(curRoomData);

            var newHisPlayerCount = 0;
            for(var j in curRoomData.player)
                newHisPlayerCount ++;
            var newHistoryItem = cc.instantiate(this.history_item);
            newHistoryItem.y = newHistoryItemY;
            this.historyItemList[i] = newHistoryItem;
            this.historyContent.addChild(newHistoryItem);
            newHistoryItem.active = true;
            newHistoryItemY += this.historyItemOffsetY;

            var score = newHistoryItem.getChildByName("score").getComponent("cc.Label");
            score.string = curRoomData.score;
            var type = newHistoryItem.getChildByName("type").getComponent("cc.Label");
            if(curRoomData.type == 201)
                type.string = "游戏结算";
            if(curRoomData.type == 301)
                type.string = "房主加分";
            if(curRoomData.type == 302)
                type.string = "房主减分";
            var hisIndex = newHistoryItem.getChildByName("index").getComponent("cc.Label");
            hisIndex.string = (parseInt(i)+1);
        }
        if(newContentHeight < 500)
            newContentHeight = 500;
        this.historyContent.height = -newHistoryItemY + 100;
    },

    showLayer:function(){
        if(this.isInit == false)
            this.onInit();
        this.node.active = true;
        this.updateHistory();
    },

    hideLayer:function(){
        this.node.active = false;
    },
});
